/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.gui.mvc.controller.testbed;

import org.testis.gui.mvc.controller.AbstractController;
import org.testis.gui.mvc.controller.TestBedControllerList;
import org.testis.gui.mvc.model.testbed.SerialConfigModel;
import org.testis.gui.mvc.view.testbed.SerialConfigView;
import org.testis.orm.testbed.datahandlers.SerialConnectionDataHandler;
import org.testis.orm.testbed.refined.ConnectionTypeFactory;
import org.testis.orm.testbed.refined.ConnectionTypeRefined;
import org.testis.orm.testbed.refined.DirectSerialConfigRefined;
import org.testis.orm.testbed.refined.NetworkSerialConfigRefined;
import org.testis.orm.testbed.refined.SerialConnectionFactory;
import org.testis.orm.testbed.refined.SerialConnectionRefined;
import org.testis.orm.testbed.refined.TestBedItem;
import org.testis.utils.tree.TbedMutableTreeNode;

/**
 *
 * @author selcuk
 */
public class SerialConfigController extends AbstractController {

    public static final String VISIBLE_PROPERTY = "Visibility";
    public static final String ERROR_PROPERTY = "LastError";
    public static final String NAME_PROPERTY = "Name";
    public static final String SERIAL_CONN_ID_PROPERTY = "SerialConnId";
    public static final String CONN_TYPE_ID_PROPERTY = "ConnTypeId";
    public static final String CONFIG_ID_PROPERTY = "ConfigId";
    private TbedMutableTreeNode m_selectedNode = null;

    public SerialConfigController() {
        super();
    }

    private void initialize(boolean editMode) {
        SerialConfigModel model = new SerialConfigModel();
        SerialConfigView view = new SerialConfigView(
                TestBedControllerList.newInstance().getParentFrame(),
                true, editMode);
        super.addModel(model);
        super.addView(view);
        view.pack();
    }

    public void operationCloseWindow() {
        super.cleanAll();
    }

    public void operationEditSerialConfig(TbedMutableTreeNode node) {
        if (node.getUserObject() instanceof SerialConnectionRefined) {
            m_selectedNode = node;
            SerialConnectionRefined refined = getSelectedConfig();
            if (refined == null) {
                System.out.println(
                        "Not found design part for SerialConnectionRefined");
                return;
            }
            initialize(true);

            setModelProperty(SERIAL_CONN_ID_PROPERTY, refined.getId());
            setModelProperty(NAME_PROPERTY, refined.getName());
            ConnectionTypeRefined connType = refined.getConnectType();
            if (connType != null) {
                setModelProperty(CONN_TYPE_ID_PROPERTY, connType.getId());
            }
            setModelProperty(CONFIG_ID_PROPERTY, refined.getConfigId());
            setModelProperty(VISIBLE_PROPERTY, true);
        }
    }

    public void operationNewSerialConfig() {
        initialize(false);
        int connTypeId = 0;
        int configId = 0;
        TestBedItem _testBedItem =
                TestBedControllerList.newInstance().getTbedTreeController().getSelectedTestBedItem();
        if (_testBedItem != null) {
            if (_testBedItem instanceof DirectSerialConfigRefined) {
                DirectSerialConfigRefined directSerialConfig = (DirectSerialConfigRefined) _testBedItem;
                ConnectionTypeRefined connType = ConnectionTypeFactory.newInstance().getConnectionType(
                        "direct");
                connTypeId = connType.getId();
                configId = directSerialConfig.getId();
            } else if (_testBedItem instanceof NetworkSerialConfigRefined) {
                NetworkSerialConfigRefined networkSerialConfig = (NetworkSerialConfigRefined) _testBedItem;
                ConnectionTypeRefined connType = ConnectionTypeFactory.newInstance().getConnectionType(
                        "network");
                connTypeId = connType.getId();
                configId = networkSerialConfig.getId();
            }
        }
        setModelProperty(CONN_TYPE_ID_PROPERTY, connTypeId);
        setModelProperty(CONFIG_ID_PROPERTY, configId);
        setModelProperty(VISIBLE_PROPERTY, true);
    }

    public void operationSaveSerialConn(int id, String name, int connTypeId, int configId) {
        boolean result = SerialConnectionDataHandler.newInstance().saveSerialConnection(
                id, name, connTypeId, configId);
        if (!result) {
            setModelProperty(ERROR_PROPERTY,
                    "Unable to edit SerialConnection record");
        }
    }

    public void operationAddSerialConn(String name, int connTypeId, int configId) {
        SerialConnectionRefined refined = SerialConnectionFactory.newInstance().create(
                name, connTypeId, configId);
        if (refined != null) {
            TestBedControllerList.newInstance().getTbedTreeController().operationAddTestBedItem(
                    refined);
        } else {
            setModelProperty(ERROR_PROPERTY,
                    "Unable to add SerialConnection record");
        }
    }

    private SerialConnectionRefined getSelectedConfig() {
        SerialConnectionRefined result = null;
        Object obj = m_selectedNode.getUserObject();
        if (obj != null && obj instanceof SerialConnectionRefined) {
            result = (SerialConnectionRefined) obj;
        }
        return result;
    }

    public void operationDeleteSerialConn(int id) {
        if (SerialConnectionFactory.newInstance().deleteConnection(id)) {
            TestBedControllerList.newInstance().getTbedTreeController().operationRemoveSelectedTestBedItem();
        } else {
            TestBedControllerList.newInstance().getTbedTreeController().operationSetErrorProperty(
                    "Unable to remove SerialConnection record");
        }
    }
}
